package com.view;

import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

public class MainView extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JSplitPane leftRightSplitPanel;
	private JSplitPane rightTopBottomSplitPanel;

	public JPanel leftPanel;
	public JPanel rightTopPanel;
	public JPanel rightBottomPanel;

	public LogTxtArea logTxt;

	private LeftTreeView leftTreeView;

	public void init() {
		setSize(1200, 800);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		leftRightSplitPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		leftRightSplitPanel.setEnabled(true);
		leftRightSplitPanel.setDividerLocation((int) (getWidth() * 0.25));

		leftPanel = new JPanel();
		leftPanel.setBorder(BorderFactory.createTitledBorder("目录"));
		leftPanel.setLayout(new BorderLayout());
		rightTopPanel = new JPanel();
		rightTopPanel.setBorder(BorderFactory.createTitledBorder("工作区"));
		rightBottomPanel = new JPanel();
		rightBottomPanel.setLayout(new BorderLayout());
		rightBottomPanel.setBorder(BorderFactory.createTitledBorder("日志"));

		logTxt = new LogTxtArea();
		logTxt.setEditable(false);
		rightBottomPanel.add(new JScrollPane(logTxt), BorderLayout.CENTER);

		rightTopBottomSplitPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		rightTopBottomSplitPanel.setEnabled(true);
		rightTopBottomSplitPanel.setDividerLocation((int) (getHeight() * 0.8));
		rightTopBottomSplitPanel.setTopComponent(rightTopPanel);
		rightTopBottomSplitPanel.setBottomComponent(rightBottomPanel);

		leftRightSplitPanel.setLeftComponent(leftPanel);
		leftRightSplitPanel.setRightComponent(rightTopBottomSplitPanel);

		add(leftRightSplitPanel, BorderLayout.CENTER);

		addLeftTree();
		setJMenuBar(new TopMenuBar(this));

		setVisible(true);
		validate();

	}

	/**
	 * 添加左侧树视图
	 */
	public void addLeftTree() {
		leftPanel.removeAll();
		leftTreeView = new LeftTreeView(this);
		leftPanel.add(leftTreeView);
		leftTreeView.expandAll(true);
	}

	public void addLog(String log) {
		logTxt.append(log + "\n");
		logTxt.setCaretPosition(logTxt.getDocument().getLength());
	}

	public void clearLog() {
		logTxt.setText("");
	}

	public static void main(String[] args) {
		new MainView().init();
	}
}
